/*
 * Copyright (c) 2016. zhaojin 15398699939@163.com
 */

package com.bankscene.screenfit.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.bankscene.screenfit.R;
import com.bankscene.screenfit.model.Menus;
import com.bankscene.screenfit.dict.ErrorDict;
import com.bankscene.screenfit.events.EventBusEvents;
import com.bankscene.screenfit.exception.PresenterModeException;
import com.bankscene.screenfit.fragment.MenusGridViewFragment;
import com.bankscene.screenfit.fragment.MenusListViewFragment;
import com.bankscene.screenfit.util.ActivityUtils;
import com.bankscene.screenfit.vus.MainVu;

import org.greenrobot.eventbus.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.bankscene.screenfit.activity.MenusActivity.LEFT_FRAGMENT;

public class MainActivity extends BasePresenterActivity<MainVu> {

    private Logger log = LoggerFactory.getLogger(MainActivity.class);

    public static final String MENU_ITEM = "menuItem";

    @Override
    protected void onBindVu() {
        setSupportActionBar(vu.getActionBar());
        ActivityUtils.addFragmentToActivity(fm, MenusGridViewFragment.newInstance(bus), R.id.contentFrame);
        // Menu item click 的監聽事件一樣要設定在 setSupportActionBar 才有作用
        vu.getActionBar().setOnMenuItemClickListener(onMenuItemClick);
    }

    @Override
    protected Class<MainVu> getVuClass() {
        return MainVu.class;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.action_search:
                case R.id.action_settings:
                    Toast.makeText(MainActivity.this, R.string.dev, Toast.LENGTH_LONG).show();
                    break;
            }
            return true;
        }
    };

    @Subscribe
    public void setOnItemClick(@NonNull EventBusEvents.OnItemClickEvent event) {
        Menus selectedMenus = Menus.getByPosition(event.position);
        Bundle paramsBundle = new Bundle();
        paramsBundle.putParcelable(MENU_ITEM, selectedMenus);
        paramsBundle.putString(LEFT_FRAGMENT, MenusListViewFragment.class.getName());
        if (!TextUtils.isEmpty(selectedMenus.getClassName())) {
            MenusActivity.newInstance(MainActivity.this, paramsBundle);
        } else if (selectedMenus.getFragments() != null && selectedMenus.getFragments().length > 0) {
            MenusTabsActivity.newInstance(MainActivity.this, paramsBundle);
        } else {
            switch (selectedMenus.getId()) {
                case R.id.logout_navigation_menu_item:
                    Snackbar.make(vu.getRootView(), getResources().getText(R.string.logout_q), Snackbar.LENGTH_LONG).setAction(getResources().getText(R.string.confirm), new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ActivityUtils.logout(activityStackManager);
                        }
                    }).show();
                    break;
                default:
                    throw new PresenterModeException(ErrorDict.ERROR_SELETEDMENUS_NOTBENULL);
            }
        }
        //不要编码
    }

    public static void newInstance(@NonNull Context fromActCtx, @Nullable Bundle paramsBundle) {
        Intent intent = new Intent(fromActCtx, MainActivity.class);
        //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        if (null != paramsBundle) {
            intent.putExtras(paramsBundle);
        }
        fromActCtx.startActivity(intent);
    }
}
